<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item>
            <el-input
              size="medium"
              v-model="foodName"
              placeholder="查询"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              size="small"
              type="primary"
              icon="el-icon-search"
              @click="getRecord"
              >查询</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button
              type="success"
              size="small"
              @click="newest(1)"
              icon="el-icon-circle-plus-outline"
              >新增</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="24">
        <div>
          <el-table
            :data="tableData"
            border
            v-loading="loadFlag"
            style="width: 100%"
          >
            <el-table-column
              prop="reimbursementPerson"
              label="报销人"
              width=""
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="department"
              label="所属部门"
              width=""
              align="center"
            >
            </el-table-column>
            <el-table-column prop="date" label="日期" align="center">
            </el-table-column>
            <el-table-column
              prop="reimbursementInstructions"
              label="报销说明"
              align="center"
            >
            </el-table-column>
            <el-table-column label="进度" align="center">
              <template #default="scope">
                <el-tag type="success" v-if="scope.row.flowState == 1"
                  >已付款</el-tag
                >
                <el-tag type="warning" v-if="scope.row.flowState == 0"
                  >进行中</el-tag
                >
                <el-tag type="danger" v-if="scope.row.flowState == 3"
                  >拒绝</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column label="操作" width="280" align="center">
              <template #default="scope">
                <el-button
                  size="mini"
                  type="success"
                  v-if="scope.row.isCanApproval == 1"
                  @click="examine(scope.row, 3)"
                  >审核</el-button
                >
                <el-button size="mini" @click="handleEdit(scope.row, 2)"
                  >查看详情</el-button
                >
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <div class="pagingClass">
            <el-pagination
              :page-sizes="m_page.sizes"
              :page-size="m_page.size"
              :current-page="m_page.number"
              @size-change="m_handleSizeChange"
              @current-change="m_handlePageChange"
              layout="total,sizes,prev, pager, next"
              background
              :total="m_page.totalElements"
            ></el-pagination>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-dialog
      :title="single"
      v-model="plassets"
      width="75%"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="报销人">
          <!-- <el-input style="width: 300px" v-model="form.people"></el-input> -->
          <el-select
            @change="peopleknew"
            v-model="form.people"
            style="width: 300px"
            filterable
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in subbuyer"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属部门">
          <el-cascader
            :show-all-levels="false"
            style="width: 300px"
            clearable
            v-model="form.superior"
            :options="department"
            :props="{ checkStrictly: true }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            style="width: 300px"
            v-model="form.dates"
            type="date"
            placeholder="选择日期"
            format="YYYY 年 MM 月 DD 日"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="用款用途">
          <el-input style="width: 300px" v-model="form.usemask"></el-input>
        </el-form-item> -->
        <el-form-item label="报销明细" style="width: 100%">
          <el-button type="success" plain @click="addLine">新增一条</el-button>
          <!-- <el-button @click="save">保存</el-button> -->
          <el-table :data="detailList" border style="width: 100%">
            <el-table-column label="内容" width="250">
              <template #default="scope">
                <el-input
                  type="textarea"
                  :rows="2"
                  v-model="scope.row.content"
                  placeholder="内容"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column label="报销金额" width="150">
              <template #default="scope">
                <el-input
                  type="number"
                  v-model.number="scope.row.amount"
                  placeholder="报销金额"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column label="发生日期" width="200">
              <template #default="scope">
                <el-date-picker
                  style="width: 170px"
                  v-model="scope.row.date"
                  type="date"
                  placeholder="选择日期"
                  format="YYYY 年 MM 月 DD 日"
                >
                </el-date-picker>
              </template>
            </el-table-column>
            <el-table-column prop="addnote" label="项目号" width="220">
              <template #default="scope">
                <el-input
                  :disabled="true"
                  style="width: 150px"
                  v-model="scope.row.projectNumber"
                  placeholder="请输入内容"
                ></el-input>
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  @click="subitem(scope.$index)"
                ></el-button>
              </template>
            </el-table-column>
            <el-table-column prop="addnote" label="备注" width="200">
              <template #default="scope">
                <el-input
                  type="textarea"
                  :rows="2"
                  v-model="scope.row.remarks"
                  placeholder="备注"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="addnote" label="上传图片" width="200">
              <el-upload
                action="https://jsonplaceholder.typicode.com/posts/"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
              <el-dialog v-model="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="" />
              </el-dialog>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template #default="scope">
                <el-button
                  type="text"
                  @click="deleteunused(scope.$index, scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="报销说明">
          <el-input
            style="width: 300px"
            v-model="form.pseudo"
            type="textarea"
            :rows="2"
          ></el-input>
        </el-form-item>
        <el-form-item label="审批进度" v-if="under == 2">
          <div style="height: 300px; margin-left: 30px">
            <el-steps
              direction="vertical"
              :active="stripes"
              v-if="passive.length"
            >
              <el-step
                v-for="item in passive"
                :title="item.nodeName"
                :description="item.remarks"
                :key="item.id"
              ></el-step>
            </el-steps>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer" style="text-align: center">
          <el-button @click="plassets = false">取 消</el-button>
          <el-button type="primary" v-if="under == 1" @click="Determine()"
            >提 交</el-button
          >
          <!-- <el-button @click="hardware()" v-if="under == 2" type="primary"
            >编辑 提交</el-button
          > -->
          <el-button type="success" @click="Disagree()" v-if="under == 3"
            >同意</el-button
          >
          <el-button type="danger" @click="RefuseSet()" v-if="under == 3"
            >拒绝</el-button
          >
        </div>
      </template>
    </el-dialog>
    <el-dialog
      title="选择项目编号"
      v-model="nserialnos"
      width="800px"
      :close-on-click-modal="false"
    >
      <getProject @wtc_serial="wtc_serial"></getProject>
    </el-dialog>
    <el-dialog
      title="拒绝理由"
      v-model="getopt"
      width="500px"
      :close-on-click-modal="false"
    >
      <el-input
        type="textarea"
        placeholder="请输入内容"
        v-model="refuse"
        maxlength="30"
        show-word-limit
      >
      </el-input>
      <template #footer>
        <div class="dialog-footer" style="text-align: center">
          <el-button @click="getopt = false">取 消</el-button>
          <el-button type="primary" @click="addition()">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import getProject from "./myProject/getProject";
import dayjs from "dayjs";
export default {
  components: {
    getProject,
  },
  data() {
    return {
      foodName: "",
      dialogImageUrl: "",
      dialogVisible: false, //上传图片
      loadFlag: false, //加载flag
      single: "",
      plassets: false,
      nserialnos: false,
      getopt: false,
      under: 1,
      refuse: "", //拒绝理由
      form: {
        superior: "", //所属部门
        people: "", //报销人
        dates: "", //日期
        // usemask: "", //用途
        pseudo: "", //说明
      },
      peoplechin: "",
      superiorchin: "",

      editors: "",
      department: [], //部门
      subbuyer: [], //人员
      options: [],
      m_page: {
        sizes: [10, 20, 40, 50, 100], //每页最大显示数
        size: 10,
        totalElements: 0,
        totalPages: 3,
        number: 1,
      },
      stripes: "",
      passive: [],
      csListIndex: null,
      underID: "",
      detailList: [
        {
          id: "",
          content: "", //内容
          amount: "", //金额
          date: "", //日期
          projectId: "",
          projectNumber: "",
          remarks: "", //备注
          file: "", //图片
        },
      ],
      tableData: [],
      approvers: {
        approvalIds: "",
        approvalState: "",
        approvalers: "",
        createId: "",
        createTime: "",
        id: "",
        isEffective: "",
        nodeName: "",
        nodeType: "",
        pId: "",
        processdesigndetailsId: "",
        sort: "",
        tableName: "",
        updateTime: "",
      },
    };
  },
  created() {
    let str = sessionStorage.getItem("token");
    this.headerObj = `Bearer ${str}`;
  },
  beforeMount() {
    this.Acquisition();
    this.production();
    this.getRecord();
  },
  methods: {
    addition() {
      this.$axios
        .post(
          `api/serial/ApprovalHistory`,
          {
            pId: this.underID,
            nodeName: this.approvers.nodeName,
            tableName: this.approvers.tableName,
            nodeType: this.approvers.nodeType,
            approvalIds: this.approvers.approvalIds,
            isEffective: this.approvers.isEffective,
            processdesigndetailsId: this.approvers.processdesigndetailsId,
            approvalState: 3,
            approvalers: this.approvers.approvalers,
            sort: this.approvers.sort,
            id: this.approvers.id,
            remarks: this.refuse,
          },
          {
            headers: {
              Authorization: this.headerObj,
            },
          }
        )
        .then(() => {
          this.$message({
            message: "拒绝",
            type: "success",
          });
          this.getRecord();
          this.plassets = false;
          this.getopt = false;
        });
    },
    //拒绝
    RefuseSet() {
      this.getopt = true;
    },
    //同意
    Disagree() {
      this.$axios
        .post(
          `api/serial/ApprovalHistory`,
          {
            pId: this.underID,
            nodeName: this.approvers.nodeName,
            tableName: this.approvers.tableName,
            nodeType: this.approvers.nodeType,
            approvalIds: this.approvers.approvalIds,
            isEffective: this.approvers.isEffective,
            processdesigndetailsId: this.approvers.processdesigndetailsId,
            approvalState: 1,
            approvalers: this.approvers.approvalers,
            sort: this.approvers.sort,
            id: this.approvers.id,
          },
          {
            headers: {
              Authorization: this.headerObj,
            },
          }
        )
        .then(() => {
          this.$message({
            message: "同意",
            type: "success",
          });
          this.getRecord();
          this.plassets = false;
        });
    },
    //审核
    examine(row, index) {
      this.plassets = true;
      this.under = index;
      this.single = "审核";
      this.underID = row.id;
      this.getProject();
      this.$axios({
        methods: "GET",
        url: "api/serial/Reimbursement",
        headers: {
          Authorization: this.headerObj,
        },
        params: {
          pageSize: 10,
          pageIndex: 1,
          query: {
            id: row.id,
          },
        },
      }).then((res) => {
        this.choose = res.data.data;
        // console.log(this.choose);
        this.form.people = this.choose.reimbursementPersonId;
        this.form.superior = this.choose.departmentId;
        this.form.dates = this.choose.date;

        this.detailList = this.choose.reimbursementDetails;
        this.form.pseudo = this.choose.reimbursementInstructions;
      });
    },
    //获取流程
    getProject() {
      this.$axios({
        methods: "GET",
        url: "api/serial/ApprovalHistory",
        headers: {
          Authorization: this.headerObj,
        },
        params: {
          pageSize: 20,
          pageIndex: 1,
          query: {
            pId: this.underID,
            tableName: "reimbursement",
          },
        },
      })
        .then((res) => {
          console.log(res);
          this.approvers = res.data.data[1];
          this.passive = res.data.data;
          let term = this.passive.reduce((count, item) => {
            console.log(item);
            if (item.approvalState == 1 || item.approvalState == 3) {
              return count + 1;
            }
            return count;
          }, 0);
          this.stripes = term;
        })
        .catch(() => {
          this.$message.error("获取失败");
        });
    },
    //删除
    handleDelete(row) {
      let into = row;
      console.log(into);
      this.$confirm("确认删除该报销单?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .post(
              `api/serial/Reimbursement`,
              {
                id: into.id,
                reimbursementPersonId: into.reimbursementPersonId, //报销人
                departmentId: into.departmentId, //所属部门
                date: into.date, //日期
                purpose: into.purpose, //用途
                // reimbursementDetails: this.detailList, //报销明细
                reimbursementInstructions: into.reimbursementInstructions, //报销说明
                isDelete: 1,
              },
              {
                headers: {
                  Authorization: this.headerObj,
                },
              }
            )
            .then(() => {
              this.getRecord();
              this.$message.success("删除成功");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //查看详情
    handleEdit(row, index) {
      this.passive = [];
      console.log(row);
      this.plassets = true;
      this.under = index;
      this.single = "查看详情";
      this.underID = row.id;
      this.getProject();
      this.$axios({
        methods: "GET",
        url: "api/serial/Reimbursement",
        headers: {
          Authorization: this.headerObj,
        },
        params: {
          pageSize: 10,
          pageIndex: 1,
          query: {
            id: row.id,
          },
        },
      }).then((res) => {
        this.choose = res.data.data;
        console.log(this.choose);
        this.form.people = this.choose.reimbursementPersonId;
        this.form.superior = this.choose.departmentId;
        this.editors = this.choose.departmentId;
        this.form.dates = this.choose.date;

        this.detailList = this.choose.reimbursementDetails;
        this.form.pseudo = this.choose.reimbursementInstructions;
      });
    },

    //新增
    Determine() {
      this.$axios
        .post(
          `api/serial/Reimbursement`,
          {
            reimbursementPerson: this.peoplechin, //报销人
            reimbursementPersonId: this.form.people, //报销人
            departmentId: this.editors, //所属部门
            department: this.superiorchin,
            date: dayjs(this.form.dates).format("YYYY-M-D"), //日期
            reimbursementDetails: this.detailList, //报销明细
            reimbursementInstructions: this.form.pseudo, //报销说明
          },
          {
            headers: {
              Authorization: this.headerObj,
            },
          }
        )
        .then(() => {
          this.$message({
            message: "保存成功",
            type: "success",
          });
          this.plassets = false;
          this.getRecord();
        })
        .catch(() => {
          this.$message.error("保存失败");
        });
    },
    //项目号
    subitem(index) {
      this.csListIndex = index;

      this.nserialnos = true;
    },
    wtc_serial(data) {
      this.detailList[this.csListIndex].projectId = data.id;
      this.detailList[this.csListIndex].projectNumber = data.serialNumber;
      this.nserialnos = false;
    },
    //获取数据
    getRecord() {
      this.loadFlag = true;
      this.$axios({
        methods: "GET",
        url: "api/serial/Reimbursement",
        headers: {
          Authorization: this.headerObj,
        },
        params: {
          pageSize: this.m_page.size,
          pageIndex: this.m_page.number,
          query: {
            reimbursementPerson: this.foodName,
          },
        },
      }).then((res) => {
        ({ data: this.tableData, count: this.m_page.totalElements } = res.data);
        this.loadFlag = false;
      });
    },
    onSubmit() {},
    //新增按钮
    newest(index) {
      this.plassets = true;
      this.single = "新增报销";
      this.under = index;
      this.notEmpty();
    },
    notEmpty() {
      this.form.people = "";
      this.editors = "";
      this.form.superior = "";
      this.form.dates = "";
      this.form.pseudo = "";
      this.detailList.forEach((item) => {
        item.id = 0;
        item.content = ""; //内容
        item.amount = ""; //金额
        item.date = ""; //日期
        item.projectId = "";
        item.projectNumber = "";
        item.remarks = ""; //备注
        item.file = ""; //图片
      });
    },
    peopleknew(value) {
      this.subbuyer.forEach((item) => {
        if (value == item.value) {
          // console.log(item);
          this.peoplechin = item.label;
        }
      });
      console.log(this.peoplechin);
    },
    handleChange() {
      if (this.form.superior.length == 1) {
        this.editors = this.form.superior[0];
      } else if (this.form.superior.length == 2) {
        this.editors = this.form.superior[1];
      } else if (this.form.superior.length == 3) {
        this.editors = this.form.superior[2];
      }
      console.log(this.editors);
      this.department.forEach((item) => {
        item.children.forEach((item1) => {
          if (this.editors == item1.value) {
            this.superiorchin = item1.label;
          }
        });
      });
      // console.log(this.superiorchin);
    },

    addLine() {
      //添加行数
      var newValue = {
        content: "", //内容
        amount: "", //金额
        date: "", //日期
        projectId: "",
        projectNumber: "",
        remarks: "", //备注
        file: "", //图片
      };
      //添加新的行数
      this.detailList.push(newValue);
    },
    // save() {
    //   //这部分应该是保存提交你添加的内容
    //   console.log(JSON.stringify(this.detailList));
    // },
    deleteunused(index) {
      //删除行数
      console.log(index);
      this.detailList.splice(index, 1);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },

    //获取人员
    production() {
      this.$axios
        .get(`api/serial/Users?pageSize=${100}&pageIndex=${1}`, {
          headers: {
            Authorization: this.headerObj,
          },
        })
        .then((res) => {
          this.dateEnd = res.data.data;
          let fwork = [];
          this.dateEnd.forEach((item) => {
            fwork.push({
              departId: item.departId,
              value: item.id,
              label: item.name,
            });
          });
          // console.log(fwork);
          this.subbuyer = fwork;
        });
    },
    //获取上级部门
    Acquisition() {
      this.$axios
        .get(`api/serial/DepartmentTree`, {
          headers: {
            Authorization: this.headerObj,
          },
        })
        .then((res) => {
          // console.log(res);
          this.fication = res.data.data;
          // console.log(this.fication);
          let arr = [];
          this.fication.forEach((item, index) => {
            arr[index] = {
              value: item.id,
              label: item.departName,
            };

            if (item.departmentList) {
              arr[index].children = [];
              item.departmentList.forEach((item1, index1) => {
                arr[index].children[index1] = {
                  value: item1.id,
                  label: item1.departName,
                };
                arr[index].children[index1].children = [];
                item1.departmentList.forEach((item2, index2) => {
                  arr[index].children[index1].children[index2] = {
                    value: item2.id,
                    label: item2.departName,
                  };
                });
              });
            }
          });

          this.department = arr;
        });
    },
    //页码
    m_handlePageChange(currPage) {
      this.m_page.number = currPage;
      this.getProducts();
    },
    m_handleSizeChange(currSize) {
      this.m_page.size = currSize;
      this.getProducts();
    },
  },
};
</script>

<style scoped>
.addccc {
  margin-bottom: 20px;
}
</style>